package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.configuration.CustomException;
import com.reggie.configuration.R;
import com.reggie.dto.SetmealDto;
import com.reggie.mapper.SetmealMapper;
import com.reggie.pojo.Setmeal;
import com.reggie.pojo.SetmealDish;
import com.reggie.service.SetmealDishService;
import com.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;


    /**
     * 新增套餐。同时新增套餐与菜品的关联关系
     * @param setmealDto
     */
    @Override
    @Transactional  //对两张表同时操作，添加事务保证数据一致性
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，对setmeal表进行insert操作
        this.save(setmealDto);

        //设置套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
         setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存菜品的关联关系，对setmeal_dish表进行insert操作
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 删除套餐，同时需要删除关联的菜品数据
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        //查询套餐状态，确定是否可删除
        //select count(*) from setmeal where id in() and status = 1;
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        long count = this.count(queryWrapper);

        if(count > 0){
            //如果不能删除抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //如果能删除，先删除套餐表中的数据
        this.removeByIds(ids);

        //再删除关系表中的数据
        //delete from setmeal_dish where setmeal_id in();
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }

    /**
     * 根据id获取套餐信息和关联菜品信息
     * @param id
     * @return
     */
    public SetmealDto getByIdWithDish(Long id){
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(id);
        //拷贝基本信息
        BeanUtils.copyProperties(setmeal,setmealDto);
        //构造条件构造器
        LambdaQueryWrapper<SetmealDish> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        setmealLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(setmealLambdaQueryWrapper);
        //给关联信息赋值
        setmealDto.setSetmealDishes(list);
        return  setmealDto;
    }

    /**
     * 修改套餐
     * @param setmealDto
     */
    @Override
    @Transactional  //对两张表操作，添加事务，保持数据一致性
    public void updateWithDishes(SetmealDto setmealDto) {
        //更新setmeal表信息
        this.updateById(setmealDto);
        //删除旧的setmeal_dish关联表信息
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Long id = setmealDto.getId();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);

        //插入新的setmeal_dish关联表信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            //设置套餐id
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}
